package avicit.im.lantop.basic.archiveRecommendFolder.service.impl;

import avicit.im.lantop.basic.archiveRecommendFolder.domain.ArchiveFolder;
import avicit.im.lantop.basic.archiveRecommendFolder.service.ArchiveRecommendFolderService;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.PojoUtil;
import avicit.platform6.core.dao.hibernate.CommonHibernateDao2;
import avicit.platform6.core.domain.BeanBase;
import avicit.platform6.core.exception.DaoException;
import avicit.platform6.core.jdbc.JdbcAvicit;
import avicit.platform6.core.properties.PlatformConstant;
import avicit.platform6.core.service.ServiceBase;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 XMW
 * @创建时间: 2019/6/5
 * @描述:
 */
@Service
public class ArchiveRecommendFolderServiceImpl extends ServiceBase implements ArchiveRecommendFolderService {

    @Override
    public void insert(BeanBase beanBase) throws DaoException {

    }

    @Override
    public void delete(BeanBase beanBase) throws DaoException {

    }

    @Override
    public void update(BeanBase beanBase) throws DaoException {

    }

    @Autowired
    private CommonHibernateDao2 hibernateDao;

    @Autowired
    private JdbcAvicit jdbcAvicit;


    /**
     * @Author XMW
     * @Description: 添加添加文件夹
     * @Param:
     * @Return:
     * @Create: 2019/6/5  15:36
     */
    @Override
	public Integer saveRecommendFolder(ArchiveFolder folder) {
        folder.setId(ComUtil.getId());
        Query query = hibernateDao.getSession().createQuery("from ArchiveFolder where orgId=?");
        query.setParameter(0, folder.getOrgId());
        List<ArchiveFolder> list = query.list();
        if (list != null && list.size() > 0) {
            //判断该文件名是否已存在
            for (ArchiveFolder archiveFolder : list) {
                if (folder.getFolderName().equals(archiveFolder.getFolderName())) {
                    return 0;
                }
            }
        }
        PojoUtil.setSysProperties(folder, PlatformConstant.OpType.insert);
        //添加评价
        hibernateDao.save(folder);
        SysLogUtil.log(folder, null, PlatformConstant.OpType.insert, PlatformConstant.OpResult.success);
        return 1;
    }


    /**
     * @Author XMW
     * @Description: 删除推荐文件夹
     * @Param:
     * @Return:
     * @Create: 2019/6/5  15:45
     */
    @Override
	public void deleteRecommendFolder(String folderIds) {
        if (folderIds != null && !"".equals(folderIds)) {
            String[] split = folderIds.split(",");
            for (String id : split) {
                ArchiveFolder folder = hibernateDao.get(ArchiveFolder.class, id);
                hibernateDao.delete(folder);
            }
        }
    }

    /**
     * @Author XMW
     * @Description: 获取推荐文件夹表格数据
     * @Param:
     * @Return:
     * @Create: 2019/6/5  15:45
     */
    @Override
	public Map<String, Object> getRecommendFolderDatagrid(String orgId, String sysId, Integer page, Integer rows) {
        Map<String, Object> resultMap = new HashMap<>();

        //获取评价总数
        long total = jdbcAvicit.getJdbcTemplate().queryForObject("SELECT COUNT(1) FROM LANTOP_ARCHIVE_FOLDER WHERE ORG_ID='" + orgId + "'",Long.class);
        StringBuffer sbSql = new StringBuffer();
        sbSql.append("SELECT * FROM (SELECT ROW_.*, ROWNUM ROWNUM_ FROM (SELECT ROW_NUMBER() OVER(ORDER BY CREATION_DATE DESC) \"sort\",T.FOLDER_NAME \"folderName\",T.ID \"id\" FROM LANTOP_ARCHIVE_FOLDER T WHERE  ORG_ID='");
        sbSql.append(orgId);
        sbSql.append("' AND SYS_ID='");
        sbSql.append(sysId);
        sbSql.append("' ) ROW_ WHERE ROWNUM <");
        sbSql.append(page * rows + 1);
        sbSql.append(" ) WHERE ROWNUM_ >");
        sbSql.append((page - 1) * rows);

        List<Map<String, Object>> list = jdbcAvicit.getJdbcTemplate().queryForList(sbSql.toString());
        resultMap.put("total", total);
        resultMap.put("rows", list);
        return resultMap;
    }
    
    
    
    /**
     * @Author XMW
     * @Description: 根据全宗获取推荐文件夹
     * @Param: 
     * @Return:
     * @Create: 2019/6/6  15:08
     */
    @Override
    public List<ArchiveFolder> getArchiveFolderByOrgId(String orgId) {
        Query query = hibernateDao.getSession().createQuery("from ArchiveFolder where orgId=?");
        query.setParameter(0, orgId);
        List<ArchiveFolder> list = query.list();
        return list;
    }
}
